<?php
function generateSecret(){
    $hash = hash_hmac('sha256', uniqid(), 'test', true);
    $iv = bin2hex(substr($hash, 0, 8));
    $key = substr(base64_encode(substr($hash, 8)), 0, 32);
    return $key . $iv;
}

$secret = generateSecret();
$key = substr($secret, 0, 32);
$iv = substr($secret, 32);

echo strlen(openssl_encrypt('123456789abcdefg', 'aes-256-cbc', $key,  OPENSSL_RAW_DATA, $iv));